Skip to content

feat: add option to omit query string from path matching and logging #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mgabeler-lee-6rs
Copy link
Contributor

Adds an option (default disabled for backwards compatibility) to change the handling of the path field to omit the query string.

When enabled:

  • The path is logged without the query string
  • The query string, if non-empty, is logged in a new query field
  • The path without the query string is used for matching in the the WithSkipPath, WithSkipPathRegexps, and WithPathLevel options

Why?

  • With structured log aggregators, being able to aggregate on the base path without highly variable query strings can be helpful
  • Overriding log levels by path is similarly likely to want to use the base path ignoring variable / user-submitted query strings

(This previously was #108, but it was closed by accident due to a push to a private repo referencing that PR)

@codecov-commenter
Copy link

codecov-commenter commented Apr 7, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.53%. Comparing base (d96c742) to head (a1ac8d3).
Report is 56 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #110      +/-   ##
==========================================
- Coverage   94.24%   90.53%   -3.72%     
==========================================
  Files           2        2              
  Lines         139      169      +30     
==========================================
+ Hits          131      153      +22     
- Misses          5       14       +9     
+ Partials        3        2       -1     
Flag Coverage Δ
go- ?
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.22 ?
go-1.23 90.53% <100.00%> (?)
go-1.24 90.53% <100.00%> (?)
macos-latest ?
ubuntu-latest 90.53% <100.00%> (-3.72%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@appleboy
Copy link
Member

Please resolve the conflicts.

@appleboy appleboy requested a review from Copilot May 20, 2025 10:36
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds an option to omit the query string from the path for logging and matching, with corresponding changes in tests, logger behavior, examples, and documentation.

  • Added a new Option function (WithPathNoQuery) to enable query-string omission.
  • Updated logging behavior in logger.go to conditionally separate the query string into its own field.
  • Extended tests and examples in logger_test.go, _example/main.go, and README.md to demonstrate this new functionality.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
options.go Introduces WithPathNoQuery to set a flag that controls query string inclusion in paths.
logger_test.go Adds tests to verify the behavior when the query string is separated from the logged path.
logger.go Updates the logging implementation to conditionally omit the query string from the path.
_example/main.go Provides an example usage of the WithPathNoQuery option.
README.md Updates documentation with an example of using WithPathNoQuery.
Comments suppressed due to low confidence (1)

options.go:273

  • [nitpick] Consider renaming the boolean parameter 'b' to a more descriptive name, such as 'enabled', to improve code clarity.
func WithPathNoQuery(b bool) Option {

@mgabeler-lee-6rs
Copy link
Contributor Author

@appleboy done!

@appleboy appleboy requested a review from Copilot May 21, 2025 15:18
@appleboy appleboy added the enhancement New feature or request label May 21, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a new option to strip query strings from the logged path and match rules, emitting the query separately when enabled.

  • Introduce WithPathNoQuery config flag and option constructor
  • Update SetLogger to conditionally remove query from path and add a query field
  • Add tests, example code, and README entries for the new behavior

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
options.go Define WithPathNoQuery option to toggle query stripping
logger.go Update path construction and log event to honor pathNoQuery
logger_test.go Add TestLoggerPathNoQuery to validate query handling
_example/main.go Example code demonstrating WithPathNoQuery usage
README.md Document example for WithPathNoQuery in the project README
Comments suppressed due to low confidence (3)

_example/main.go:177

  • [nitpick] The variable name nq is very short and may be unclear in example code. Consider renaming it to something more descriptive, like noQueryGroup, to improve readability.
nq := r.Group("/no-query", logger.SetLogger(

README.md:140

  • [nitpick] In README examples, nq is a terse variable name. Using a more descriptive name such as noQueryGroup will help readers understand its purpose.
  nq := r.Group("/no-query", logger.SetLogger(

logger_test.go:371

  • The test checks for the presence of the query field but doesn't verify that the path field is free of the query component. Adding an assertion to confirm path excludes ?foo=bar when WithPathNoQuery(true) would strengthen coverage.
		for i, line := range lines {

@@ -136,6 +136,23 @@ func main() {
})
}

// Example of WithPathNoQuery usage
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a sample of the actual logged output after this example block. Showing how path and query fields appear will make the feature's effect more concrete.

Copilot uses AI. Check for mistakes.

@@ -157,17 +163,22 @@ func SetLogger(opts ...Option) gin.HandlerFunc {

start := time.Now()
path := c.Request.URL.Path
if raw := c.Request.URL.RawQuery; raw != "" {
path += "?" + raw
rawQuery := c.Request.URL.RawQuery
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The logic for extracting and handling the query string is duplicated in two places. Extracting path and query parsing into a small helper function would reduce duplication and improve maintainability.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants